//
//  UIImage+MJ.m
//  新浪微博
//
//  Created by apple on 13-10-26.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "UIImage+MJ.h"
//#import "NSString+MJ.h"

@implementation UIImage (MJ)
#pragma mark 加载全屏的图片
// new_feature_1.png
+ (UIImage *)fullscrennImage:(NSString *)imgName
{
    // 1.如果是iPhone5，对文件名特殊处理
    
    // 2.加载图片
    return [self imageNamed:imgName];
}

#pragma mark 可以自由拉伸的图片
+ (UIImage *)resizedImage:(NSString *)imgName
{
    return [self resizedImage:imgName xPos:0.5 yPos:0.5];
}

+ (UIImage *)resizedImage:(NSString *)imgName xPos:(CGFloat)xPos yPos:(CGFloat)yPos
{
    UIImage *image = [UIImage imageNamed:imgName];
    return [image stretchableImageWithLeftCapWidth:image.size.width * xPos topCapHeight:image.size.height * yPos];
}


+(void)loadImageWithUrlStr:(NSString *)ImageUrl loadSuccessBlock:(void (^)(NSData *successData))successBlock loadFailureBlock:(void (^)())failureBlock
{
    dispatch_queue_t queue = dispatch_queue_create("loadImage", NULL);
    
    dispatch_async(queue, ^{
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageUrl]];
        
        if (data.length != 0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                successBlock(data);
            });
            
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                failureBlock();
            });
            
        }
    });
    
}

+(UIImage *)imageWithColor:(UIColor *)color{
    return  [self imageWithColor:color size:CGSizeMake(1, 1)];
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}



+ (UIImage *)hq_imageWithName:(NSString *)imageName Ratio:(CGFloat)ratio{
    
    UIImage *image = [UIImage imageNamed:imageName];
    if (ratio <= 1.0) {
        UIGraphicsBeginImageContext(CGSizeMake(image.size.width , image.size.height / ratio));
        [image drawInRect:CGRectMake(0, ((image.size.height / ratio) - image.size.height) / 2, image.size.width, image.size.height)];
    }else{
        UIGraphicsBeginImageContext(CGSizeMake(image.size.width * ratio , image.size.height));
        [image drawInRect:CGRectMake((image.size.width * ratio - image.size.width) / 2, 0, image.size.width, image.size.height)];
    }
    
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return reSizeImage;
}


- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetAlpha(ctx, alpha);
    CGContextDrawImage(ctx, area, self.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
@end
